package com.example.fw;


public class ContactHelper extends HelperBase {

	public ContactHelper(ApplicationManager manager) {
		super (manager);
	}

	public void createContact(Contact contact) {
		initContactCreation();
		fillContactForm(contact);
		confirmContactCreation();
	}

	private void initContactCreation() {
		manager.getAutoItHelper()
			.winWaitAndActivate("AddressBook Portable", "", winWaitTimeout)
			.click("Add")
			.winWaitAndActivate("Add Contact", "", winWaitTimeout);
	}
	
	private void fillContactForm(Contact contact) {
		manager.getAutoItHelper()
			.send("TDBEdit12", contact.firstName)
			.send("TDBEdit11", contact.lastName);
	}

	private void confirmContactCreation() {
		manager.getAutoItHelper()
			.click("Save")
			.winWaitAndActivate("AddressBook Portable", "", winWaitTimeout);
	}

	public Contact getFirstContact() {
		selectContact();
		manager.getAutoItHelper()
			.click("Edit")
			.winWaitAndActivate("Update Contact", "", winWaitTimeout);
		Contact contact = new Contact()
			.setFirstName(manager.getAutoItHelper().getText("TDBEdit12"))
			.setLastName(manager.getAutoItHelper().getText("TDBEdit11"));
		manager.getAutoItHelper()
			.click("Cancel")
			.winWaitAndActivate("AddressBook Portable", "", winWaitTimeout);
		return contact;
	}

	public boolean  checkContactsAbsence(){
		selectContact();
		manager.getAutoItHelper()
			.click("Edit");
		if (!manager.getAutoItHelper().winExists("Information", "", winWaitTimeout)) {
			manager.getAutoItHelper()
				.winWaitAndActivate("Update Contact", "", winWaitTimeout)
				.click("Cancel");
			return false;
		} else {
			manager.getAutoItHelper()
					.winWaitAndActivate("Information", "", winWaitTimeout)
					.click("OK")
					.winWaitAndActivate("AddressBook Portable", "", winWaitTimeout);; 
		}
		return true;
	}
	
	public void deleteContact() {
		selectContact();
		manager.getAutoItHelper()
		.click("Delete")
		.winWaitAndActivate("Confirm", "", winWaitTimeout)
		.click("&Yes")
		.winWaitAndActivate("AddressBook Portable", "", winWaitTimeout);
	}

	private void selectContact() {
		manager.getAutoItHelper()
		.winWaitAndActivate("AddressBook Portable", "", winWaitTimeout)
		.click("TListView1")
		.send("{DOWN}{SPACE}");
	}
}
